<template>
  <div class="home-page">
    <!-- 顶部导航栏 -->
    <div class="top-nav">
      <div class="nav-left">
        <div class="logo">⚖️ 维权力</div>
      </div>
      <div class="nav-right">
        <div v-if="!userStore.isLoggedIn" class="login-btn" @click="goToLogin">
          登录
        </div>
        <div v-else class="user-avatar" @click="goToProfile">
          <img :src="userStore.userInfo?.avatar || defaultAvatar" alt="头像" />
        </div>
      </div>
    </div>

    <!-- 搜索栏 -->
    <div class="search-bar">
      <van-search
        v-model="searchValue"
        placeholder="搜索维权案例、模板"
        @search="onSearch"
        @click="$router.push('/search')"
        readonly
      />
    </div>

    <!-- 快捷入口 -->
    <div class="quick-actions">
      <div class="action-grid">
        <div class="action-item" @click="$router.push('/ai/diagnosis')">
          <div class="action-icon ai-icon">🤖</div>
          <div class="action-text">AI诊断</div>
        </div>
        <div class="action-item" @click="$router.push('/templates')">
          <div class="action-icon template-icon">📄</div>
          <div class="action-text">模板库</div>
        </div>
        <div class="action-item" @click="$router.push('/assistance')">
          <div class="action-icon help-icon">🆘</div>
          <div class="action-text">找援助</div>
        </div>
        <div class="action-item" @click="$router.push('/community')">
          <div class="action-icon community-icon">👥</div>
          <div class="action-text">维权社区</div>
        </div>
      </div>
    </div>

    <!-- 高频场景推荐 -->
    <div class="hot-scenarios">
      <div class="section-title">
        <h3>高频维权场景</h3>
        <span class="more" @click="$router.push('/community')">更多</span>
      </div>
      <div class="scenario-list">
        <div 
          v-for="scenario in hotScenarios" 
          :key="scenario.id"
          class="scenario-item"
          @click="handleScenarioClick(scenario)"
        >
          <div class="scenario-icon">{{ scenario.icon }}</div>
          <div class="scenario-content">
            <div class="scenario-title">{{ scenario.title }}</div>
            <div class="scenario-desc">{{ scenario.desc }}</div>
            <div class="scenario-stats">
              <span class="help-count">{{ scenario.helpCount }}人求助</span>
              <span class="success-rate">成功率{{ scenario.successRate }}%</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 精选成功案例 -->
    <div class="success-cases">
      <div class="section-title">
        <h3>精选成功案例</h3>
        <span class="more" @click="$router.push('/community?filter=resolved')">更多</span>
      </div>
      <div class="case-list">
        <div 
          v-for="case_ in successCases" 
          :key="case_.id"
          class="case-item"
          @click="$router.push(`/community/post/${case_.id}`)"
        >
          <div class="case-header">
            <span class="case-tag">{{ case_.category }}</span>
            <span class="case-status resolved">已解决</span>
          </div>
          <div class="case-title">{{ case_.title }}</div>
          <div class="case-summary">{{ case_.summary }}</div>
          <div class="case-footer">
            <span class="case-amount">追回{{ case_.amount }}</span>
            <span class="case-time">{{ case_.resolveTime }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部导航 -->
    <van-tabbar v-model="activeTab" @change="onTabChange">
      <van-tabbar-item icon="home-o" to="/home">首页</van-tabbar-item>
      <van-tabbar-item icon="chat-o" to="/community">社区</van-tabbar-item>
      <van-tabbar-item icon="orders-o" to="/templates">工具</van-tabbar-item>
      <van-tabbar-item icon="user-o" to="/profile">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const searchValue = ref('')
const activeTab = ref(0)
const defaultAvatar = 'https://img.yzcdn.cn/vant/cat.jpeg'

// 高频场景数据
const hotScenarios = ref([
  {
    id: 1,
    icon: '💰',
    title: '工资拖欠',
    desc: '公司拖欠工资超过30天',
    helpCount: 1234,
    successRate: 85
  },
  {
    id: 2,
    icon: '🏠',
    title: '押金不退',
    desc: '租房押金无理由不退还',
    helpCount: 856,
    successRate: 78
  },
  {
    id: 3,
    icon: '🛒',
    title: '商品质量',
    desc: '购买商品存在质量问题',
    helpCount: 642,
    successRate: 92
  }
])

// 成功案例数据
const successCases = ref([
  {
    id: 1,
    category: '职场纠纷',
    title: '成功追回3个月拖欠工资',
    summary: '通过劳动仲裁，3个月内成功追回被拖欠的工资和赔偿金',
    amount: '2.8万元',
    resolveTime: '3个月前'
  },
  {
    id: 2,
    category: '租房矛盾',
    title: '房东恶意扣押金，成功维权',
    summary: '房东以各种理由扣押金，通过投诉和协商成功要回',
    amount: '5000元',
    resolveTime: '1个月前'
  }
])

// 搜索处理
const onSearch = (value) => {
  router.push(`/search?q=${encodeURIComponent(value)}`)
}

// 登录处理
const goToLogin = () => {
  router.push('/login')
}

// 个人中心处理
const goToProfile = () => {
  router.push('/profile')
}

// 场景点击处理
const handleScenarioClick = (scenario) => {
  router.push(`/ai/diagnosis?scenario=${scenario.id}`)
}

// 底部导航切换
const onTabChange = (index) => {
  activeTab.value = index
}

onMounted(() => {
  // 页面加载时的初始化逻辑
})
</script>

<style scoped>
.home-page {
  min-height: 100vh;
  background: #f7f8fa;
  padding-bottom: 50px;
}

.top-nav {
  background: white;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #eee;
}

.nav-left .logo {
  font-size: 18px;
  font-weight: 600;
  color: #1989fa;
}

.nav-right .login-btn {
  background: #1989fa;
  color: white;
  padding: 6px 16px;
  border-radius: 16px;
  font-size: 14px;
  cursor: pointer;
}

.nav-right .user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
}

.nav-right .user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.search-bar {
  background: white;
  padding: 8px 16px;
  border-bottom: 1px solid #eee;
}

.quick-actions {
  background: white;
  margin: 8px 16px;
  border-radius: 12px;
  padding: 20px;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
}

.action-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin-bottom: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.action-text {
  font-size: 12px;
  color: #323233;
  font-weight: 500;
}

.hot-scenarios, .success-cases {
  margin: 16px;
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.section-title h3 {
  font-size: 16px;
  font-weight: 600;
  color: #323233;
}

.more {
  font-size: 14px;
  color: #1989fa;
  cursor: pointer;
}

.scenario-list, .case-list {
  background: white;
  border-radius: 12px;
  overflow: hidden;
}

.scenario-item {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
}

.scenario-item:last-child {
  border-bottom: none;
}

.scenario-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: #f7f8fa;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  margin-right: 12px;
}

.scenario-content {
  flex: 1;
}

.scenario-title {
  font-size: 14px;
  font-weight: 600;
  color: #323233;
  margin-bottom: 4px;
}

.scenario-desc {
  font-size: 12px;
  color: #969799;
  margin-bottom: 8px;
}

.scenario-stats {
  display: flex;
  gap: 12px;
}

.help-count, .success-rate {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
}

.help-count {
  background: #fff7e6;
  color: #ff976a;
}

.success-rate {
  background: #f0f9ff;
  color: #1989fa;
}

.case-item {
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
}

.case-item:last-child {
  border-bottom: none;
}

.case-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.case-tag {
  font-size: 12px;
  padding: 2px 8px;
  background: #f0f9ff;
  color: #1989fa;
  border-radius: 4px;
}

.case-status.resolved {
  font-size: 12px;
  padding: 2px 8px;
  background: #f6ffed;
  color: #52c41a;
  border-radius: 4px;
}

.case-title {
  font-size: 14px;
  font-weight: 600;
  color: #323233;
  margin-bottom: 6px;
}

.case-summary {
  font-size: 12px;
  color: #969799;
  line-height: 1.4;
  margin-bottom: 8px;
}

.case-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.case-amount {
  font-size: 12px;
  color: #ee0a24;
  font-weight: 600;
}

.case-time {
  font-size: 12px;
  color: #c8c9cc;
}
</style>